package com.example.orderfood2.RecyclerViewAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.orderfood2.R;
import com.example.orderfood2.entity.Student;

import java.util.List;

public class RankAdapter extends RecyclerView.Adapter<RankAdapter.RankViewHolder>{
    private List<Student> studentList;
    public RankAdapter(List<Student> studentList){
        this.studentList=studentList;
    }
    public static class RankViewHolder extends RecyclerView.ViewHolder {
        TextView tvRank;
        TextView tvUsername;
        TextView tvScore;

        public RankViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRank = itemView.findViewById(R.id.tv_student_rank);
            tvUsername = itemView.findViewById(R.id.tv_student_username);
            tvScore = itemView.findViewById(R.id.tv_score);
        }
    }
    @NonNull
    @Override
    public RankAdapter.RankViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_class_rank, parent, false);
        return new RankViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RankAdapter.RankViewHolder holder, int position) {
        Student student = studentList.get(position);
        // 排名是position+1（因为position从0开始）
        holder.tvRank.setText(String.valueOf(position + 1));
        holder.tvUsername.setText(student.getUsername());
        holder.tvScore.setText(student.getScore());
    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }
}
